package cn.jf.modules.system.service;

import cn.jf.common.constant.CacheConstant;
import cn.jf.common.system.util.RedisUtil;
import cn.jf.common.system.util.oConvertUtils;
import cn.jf.modules.system.entity.SysRolePermission;
import cn.jf.modules.system.entity.SysUser;
import cn.jf.modules.system.mapper.SysRolePermissionMapper;
import cn.jf.modules.system.mapper.SysUserRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;

import java.util.*;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @Author jf
 * @since 2018-12-21
 */
@Service(version = "1.0.0")
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private SysUserRoleMapper userRoleMapper;
	@Autowired
	private ISysUserService userService;
	@Override
	@CacheEvict(value= CacheConstant.LOGIN_USER_RULES_CACHE, allEntries=true)
	public void saveRolePermission(String roleId, String permissionIds) {
		List<String> stringList = userRoleMapper.getUserIdByRoleId(roleId);
		if(stringList.size()>0){
			for(String s : stringList){
				SysUser sysUser = userService.getById(s);
				if(null != sysUser) {
					redisUtil.del(CacheConstant.PERMISSION_CACHE + sysUser.getUsername());
				}
			}
		}
		SysRolePermission sysRolePermission = new SysRolePermission();
		sysRolePermission.setRoleId(roleId);
		this.baseMapper.deleteByEntity(sysRolePermission);
		List<SysRolePermission> list = new ArrayList<SysRolePermission>();
		String arr[] = permissionIds.split(",");
		for (String p : arr) {
			if(oConvertUtils.isNotEmpty(p)) {
				SysRolePermission rolepms = new SysRolePermission(roleId, p);
				list.add(rolepms);
			}
		}
		this.saveBatch(list);
	}

	@Override
	@CacheEvict(value= CacheConstant.LOGIN_USER_RULES_CACHE, allEntries=true)
	public void saveRolePermission(String roleId, String permissionIds, String lastPermissionIds) {
		List<String> stringList = userRoleMapper.getUserIdByRoleId(roleId);
		if(stringList.size()>0){
			for(String s : stringList){
				SysUser sysUser = userService.getById(s);
				if(null != sysUser) {
					redisUtil.del(CacheConstant.PERMISSION_CACHE + sysUser.getUsername());
				}
			}
		}
		List<String> add = getDiff(lastPermissionIds,permissionIds);
		if(add!=null && add.size()>0) {
			List<SysRolePermission> list = new ArrayList<SysRolePermission>();
			for (String p : add) {
				if(oConvertUtils.isNotEmpty(p)) {
					SysRolePermission rolepms = new SysRolePermission(roleId, p);
					list.add(rolepms);
				}
			}
			this.saveBatch(list);
		}
		
		List<String> delete = getDiff(permissionIds,lastPermissionIds);
		if(delete!=null && delete.size()>0) {
			for (String permissionId : delete) {
				SysRolePermission sysRolePermission = new SysRolePermission();
				sysRolePermission.setRoleId(roleId);
				sysRolePermission.setPermissionId(permissionId);
				this.baseMapper.deleteByEntity(sysRolePermission);
			}
		}
	}
	
	/**
	 * 从diff中找出main中没有的元素
	 * @param main
	 * @param diff
	 * @return
	 */
	private List<String> getDiff(String main,String diff){
		if(oConvertUtils.isEmpty(diff)) {
			return null;
		}
		if(oConvertUtils.isEmpty(main)) {
			return Arrays.asList(diff.split(","));
		}
		
		String[] mainArr = main.split(",");
		String[] diffArr = diff.split(",");
		Map<String, Integer> map = new HashMap<>();
		for (String string : mainArr) {
			map.put(string, 1);
		}
		List<String> res = new ArrayList<String>();
		for (String key : diffArr) {
			if(oConvertUtils.isNotEmpty(key) && !map.containsKey(key)) {
				res.add(key);
			}
		}
		return res;
	}
	public List<SysRolePermission> getList(SysRolePermission sysRolePermission){
		return this.baseMapper.getList(sysRolePermission);
	}

	@Override
	public SysRolePermission getOnlyOne(SysRolePermission sysRolePermission) {
		return this.baseMapper.getOnlyOne(sysRolePermission);
	}
}
